//useBrandsQueryStore.ts
import { create } from "zustand";
import { useQuery } from "react-query";
import { useMemo } from "react";
import { RefinementsQueryStore } from "./type";

export const refinementsBaseUrl = "http://localhost:4000/clothing/refinements";

export const useRefinementsQueryStore = create<RefinementsQueryStore>(() => ({
  refinements: [],
}));

export function useRefinementsAdminProducts() {
  const { data, isLoading, isError, isFetching } = useQuery(
    "refinements",
    async () => {
      const response = await fetch(refinementsBaseUrl);
      if (!response.ok) {
        throw new Error("Network response was not ok");
      }
      const data = await response.json();
      return data;
    }
  );

  const refinementsAdminProducts = useMemo(() => data || [], [data]);

  return { refinementsAdminProducts, isLoading, isError, isFetching };
}
